package com.example.mobile2021606185zhn0408class;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private CheckBox chkboxMainYinqing;
    private CheckBox chkboxMainBoli;
    private CheckBox chkboxMainFangxiangpan;
    private Button btnMainBuy;
    private TextView tvMainTip;
    private TextView tvMainResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        chkboxMainYinqing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    tvMainResult.setText("选中："+buttonView.getText().toString());
                    Toast.makeText(MainActivity.this,"已选中轿车引擎",Toast.LENGTH_SHORT).show();
                }else{
                    tvMainResult.setText("取消选中"+buttonView.getText().toString());
                    Toast.makeText(MainActivity.this,"已取消轿车引擎",Toast.LENGTH_SHORT).show();
                }
            }
        });
        chkboxMainBoli.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    tvMainResult.setText("选中："+buttonView.getText().toString());
                    Toast.makeText(MainActivity.this,"已选中轿车玻璃",Toast.LENGTH_SHORT).show();
                }else{
                    tvMainResult.setText("取消选中："+buttonView.getText().toString());
                    Toast.makeText(MainActivity.this,"已取消轿车玻璃",Toast.LENGTH_SHORT).show();
                }
            }
        });
        chkboxMainFangxiangpan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    tvMainResult.setText("选中："+buttonView.getText().toString());
                    Toast.makeText(MainActivity.this,"已选中方向盘",Toast.LENGTH_SHORT).show();
                }else{
                    tvMainResult.setText(null);
                    Toast.makeText(MainActivity.this,"已取消方向盘",Toast.LENGTH_SHORT).show();
                }

            }
        });
        btnMainBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mgs = "选中：";
                if (chkboxMainYinqing.isChecked()){
                  mgs += "\n"+chkboxMainYinqing.getText().toString();
                }
                if (chkboxMainBoli.isChecked()){
                    mgs += "\n"+chkboxMainBoli.getText().toString();
                }
                if (chkboxMainFangxiangpan.isChecked()){
                    mgs += "\n"+chkboxMainFangxiangpan.getText().toString();
                }
                tvMainResult.setText(mgs);
            }

        });
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        chkboxMainYinqing = (CheckBox) findViewById(R.id.chkbox_main_yinqing);
        chkboxMainBoli = (CheckBox) findViewById(R.id.chkbox_main_boli);
        chkboxMainFangxiangpan = (CheckBox) findViewById(R.id.chkbox_main_fangxiangpan);
        btnMainBuy = (Button) findViewById(R.id.btn_main_buy);
        tvMainTip = (TextView) findViewById(R.id.tv_main_tip);
        tvMainResult = (TextView) findViewById(R.id.tv_main_result);
    }
}